<?php
include_once LIB_PATH . 'cls.addon.php';

class Upload extends Addon
{
  public $img_quality = IMG_QUALITY;
  public $img_size = IMG_SIZE;
  public $img_ext = IMG_EXT;
  public $media_size = MEDIA_SIZE;
  public $media_ext = MEDIA_EXT;
  public $file_size = FILE_SIZE;
  public $file_ext = FILE_EXT;

  function __construct()
  {
    echo '<script type="text/javascript" src="/static/js/plupload/plupload.full.min.js"></script>' . PHP_EOL;
    echo '<script type="text/javascript" src="/static/js/plupload/i18n/zh_CN.js"></script>' . PHP_EOL;
  }

  public function plupload()
  {
    $arr = cms('plupload');
    if (!empty($arr)) {
      echo "<script>" . PHP_EOL;
      foreach ($arr as $key => $value) {
        list($type, $id) = $value;
        $ipt = str_replace("_upload", "", $id);
        echo "var uploader{$key} = new plupload.Uploader({runtimes: 'html5,flash,silverlight,html4', browse_button : '" . ltrim($id, '#') . "', flash_swf_url : '/static/js/plupload/Moxie.swf', silverlight_xap_url : '/static/js/plupload/Moxie.xap',";
        switch ($type) {
          case 'image':
            echo "url : '?s=admin&m=upload&c=image', multi_selection: false, resize: {quality: " . $this->img_quality . "}, filters : { max_file_size : '" . $this->img_size . "', mime_types: [ {title : 'Image files', extensions : '" . $this->img_ext . "'} ] }";
            break;
          case 'multiimage':
            echo "url : '?s=admin&m=upload&c=image', multi_selection: true, resize: {quality: " . $this->img_quality . "}, filters : { max_file_size : '" . $this->img_size . "', mime_types: [ {title : 'Image files', extensions : '" . $this->img_ext . "'} ] }";
            break;
          case 'media':
            echo "url : '?s=admin&m=upload&c=media', filters : { max_file_size : '" . $this->media_size . "', mime_types: [ {title : 'Media files', extensions : '" . $this->media_ext . "'} ] },";
            break;
          default:
            //file
            echo "url : '?s=admin&m=upload&c=file', filters : { max_file_size : '" . $this->file_size . "', mime_types: [ {title : 'File files', extensions : '" . $this->file_ext . "'} ] }";
            break;
        }
        echo "});" . PHP_EOL;
        echo "uploader{$key}.init();" . PHP_EOL;
        echo "uploader{$key}.bind('FilesAdded',function(uploader{$key}, files){
          // plupload.each(files, function(file) {
            // console.log('id:'+file.id+' name:'+file.name);
          // });
          uploader{$key}.start();
        });" . PHP_EOL;
        echo "uploader{$key}.bind('FileUploaded',function(uploader{$key}, file, data){
          // console.log(data);
          var obj = JSON.parse(data.response);
          console.log(obj);";
        if ($type == 'multiimage') {
          echo "var iptval = $('{$ipt}').val();
            iptval += (iptval=='' ? obj.info.path : '|' + obj.info.path);
            $('{$ipt}').val(iptval);";
        } else {
          echo "$('{$ipt}').val(obj.info.path);";
        }
        echo "})" . PHP_EOL;
        echo "uploader{$key}.bind('Error',function(uploader{$key}, data){
          alert(data.message);
        });" . PHP_EOL;
      }
      echo "</script>" . PHP_EOL;
    }
  }
}